package snmp

import (
	"pids-cloud-server/entity"
	"pids-cloud-server/logging"
)

// SnmpPduDataAcquisition /*pdu采集snmp实现*/
type SnmpPduDataAcquisition struct {
	SnmpDataAcquisition
}

func (s *SnmpPduDataAcquisition) AcquirePduVoltage(snmpOidMap map[string]entity.SnmpOid) ([]PduStateInfo, error) {
	var pduStateInfos []PduStateInfo
	g := s.Snmpclient
	if snmpOidMap[PduL1].OidValue != "" {
		L1list, err := g.BulkWalkAll(snmpOidMap[PduL1].OidValue)
		if err != nil {
			logging.Error("walk PduL1 error:", err)
			return pduStateInfos, err
		}
		pduStateInfos = append(pduStateInfos, PduStateInfo{
			Name:        snmpOidMap[PduL1].Descr,
			Current:     string(L1list[0].Value.([]byte)),
			Voltage:     string(L1list[1].Value.([]byte)),
			Power:       string(L1list[2].Value.([]byte)),
			PowerFactor: string(L1list[3].Value.([]byte)),
			Energy:      string(L1list[4].Value.([]byte)),
		})
	}
	if snmpOidMap[PduL2].OidValue != "" {
		L2list, err := g.BulkWalkAll(snmpOidMap[PduL2].OidValue)
		if err != nil {
			logging.Error("walk PduL2 error:", err)
			return pduStateInfos, err
		}
		pduStateInfos = append(pduStateInfos, PduStateInfo{
			Name:        snmpOidMap[PduL2].Descr,
			Current:     string(L2list[0].Value.([]byte)),
			Voltage:     string(L2list[1].Value.([]byte)),
			Power:       string(L2list[2].Value.([]byte)),
			PowerFactor: string(L2list[3].Value.([]byte)),
			Energy:      string(L2list[4].Value.([]byte)),
		})
	}
	if snmpOidMap[PduL3].OidValue != "" {
		L3list, err := g.BulkWalkAll(snmpOidMap[PduL3].OidValue)
		if err != nil {
			logging.Error("walk PduL3 error:", err)
			return pduStateInfos, err
		}
		pduStateInfos = append(pduStateInfos, PduStateInfo{
			Name:        snmpOidMap[PduL3].Descr,
			Current:     string(L3list[0].Value.([]byte)),
			Voltage:     string(L3list[1].Value.([]byte)),
			Power:       string(L3list[2].Value.([]byte)),
			PowerFactor: string(L3list[3].Value.([]byte)),
			Energy:      string(L3list[4].Value.([]byte)),
		})
	}
	return pduStateInfos, nil
}

func (s *SnmpPduDataAcquisition) AcquirePduSocket(snmpOidMap map[string]entity.SnmpOid) ([]PduSocketInfo, error) {
	var pduSocketInfos []PduSocketInfo
	g := s.Snmpclient
	result, err := g.Get([]string{snmpOidMap[PDU_OUTPUT_NUMBER].OidValue})
	if err != nil {
		logging.Error("get PDU_OUTPUT_NUMBER error:", err)
	}
	var (
		outputSwitchList  []string
		outputCurrentList []string
	)
	portnumber := result.Variables[0].Value.(int)
	if snmpOidMap[PDU_OUTPUT_CURRENT].OidValue != "" {
		currentlist, err := g.BulkWalkAll(snmpOidMap[PDU_OUTPUT_CURRENT].OidValue)
		if err != nil {
			logging.Error("walk PDU_OUTPUT_CURRENT error:", err)
		}
		for _, pdu := range currentlist {
			outputCurrentList = append(outputCurrentList, string(pdu.Value.([]byte)))
		}
	}
	if snmpOidMap[PDU_OUTPUT_SWITCH].OidValue != "" {
		switchlist, err := g.BulkWalkAll(snmpOidMap[PDU_OUTPUT_SWITCH].OidValue)
		if err != nil {
			logging.Error("walk PDU_OUTPUT_SWITCH error:", err)
		}
		for _, pdu := range switchlist {
			outputSwitchList = append(outputSwitchList, string(pdu.Value.([]byte)))
		}
	}
	if len(outputCurrentList) > 0 && len(outputSwitchList) > 0 {
		for i := 0; i < portnumber; i++ {
			pduSocketInfos = append(pduSocketInfos, PduSocketInfo{
				OutputCurrent: outputCurrentList[i],
				OutputSwitch:  outputSwitchList[i],
			})
		}
	}
	return pduSocketInfos, nil
}
